<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class getRandomModels extends CWidget {

    public function run() {
        $data = Controller::$pageData;
//        $sql = " SELECT \"pageUrl\",\"pageTitle\",\"pageImages\" FROM {{pages}} WHERE \"pageParent\"=" . Controller::$pageData['_parents']['brand']['pageId'] . " AND \"pageType\"='model' AND \"pageId\"!=" . Controller::$pageData['pageId'] . " ORDER BY RANDOM() LIMIT 10 ";
        $mac = '';
        if (Controller::$pageData['pageId'] == 3139 || Controller::$pageData['pageId'] == 3033) {
            $mac = ' AND "pageTitle" ILIKE \'%' . Controller::$pageData['pageTitle'] . '%\' ';
        }
        
        $sql = 'SELECT
                                *
                               FROM
                                (
                                 (
                                  SELECT
                                   *
                                  FROM
                                   mb_pages
                                  WHERE
                                   "pageSort" < ' . Controller::$pageData['pageSort'] . '
                                  AND "pageParent" = ' . Controller::$pageData['_parents']['brand']['pageId'] . '
                                       ' . $mac . '
                                  ORDER BY
                                   "pageSort" DESC
                                  LIMIT 10
                                 )
                                 UNION
                                  (
                                   SELECT
                                    *
                                   FROM
                                    mb_pages
                                   WHERE
                                    "pageSort" > ' . Controller::$pageData['pageSort'] . '
                                   AND "pageParent" = ' . Controller::$pageData['_parents']['brand']['pageId'] . '
                                       ' . $mac . '
                                   ORDER BY
                                    "pageSort" ASC
                                   LIMIT 10
                                  )
                                ) T
                               ORDER BY
                                "pageSort" ASC
                               LIMIT 10';
//        echo $sql;
        $models = Yii::app()->db->createCommand($sql)->queryAll(true);
        $this->render('randommodels', ['models' => $models]);
    }

}
